package io.xxx.mario.task;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.xxx.mario.common.PageRequest;
import io.xxx.mario.task.api.CreateTaskRequest;
import io.xxx.mario.task.api.UpdateTaskRequest;
import jakarta.annotation.Resource;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

@Tag(name = "Task", description = "任务管理")
@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private TaskService taskService;

    /**
     * 查询任务列表
     */
    @GetMapping("/list")
    public Page<Task> getPage(
            @RequestParam(required = false, defaultValue = "1") int pageNo,
            @RequestParam(required = false, defaultValue = "20") int pageSize,
            @RequestParam(required = false) String name
    ) {
        return taskService.getPage(new PageRequest(pageNo, pageSize), name);
    }

    /**
     * 查询任务详情
     */
    @GetMapping("/{id:\\d+}")
    public Task get(@PathVariable Long id) {
        return taskService.get(id);
    }

    /**
     * 保存任务信息
     */
    @PostMapping("/create")
    public Long create(@RequestBody CreateTaskRequest request) throws SchedulerException {
        return taskService.create(request);
    }

    @PutMapping("/update")
    public void update(@RequestBody UpdateTaskRequest request) {
        taskService.update(request);
    }
}
